<?php
// +----------------------------------------------------------------------
// | Action ：报名
// +----------------------------------------------------------------------
// | DateTime：2015年12月8日
// +----------------------------------------------------------------------
// | Author: Danson <zoudan916@163.com>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Admin\LordModel;
use Admin\PictureModel;
use Home\VoetModel;
use Think\Image;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class LordController extends WapController {

    public function action(){
        return 'Lord/index';
    }

    /* 报名 */
    public function index(){
        $user = M('lord_user')->where(array('openid'=>$_SESSION['wx_user']['openid']))->find();
        $info = M('lord_user')->where(array('lord_id'=>I('get.lord_id')))->find();
        $this->assign('info',$info);
        $this->assign('user',$user);
        $this->display();
    }

    /* 报名成功 */
    public function apply_ok(){
        $Picture = D('Admin/Picture');
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $lord_img = $Picture->upload(
            $_FILES,
            C('PICTURE_UPLOAD'),
            C('PICTURE_UPLOAD_DRIVER'),
            C("UPLOAD_{$pic_driver}_CONFIG")
        );
        $image = new \Think\Image();
        $path = './Uploads/'.$lord_img['choose']['path'];
        $image->open('./Uploads/'.$lord_img['choose']['path']);
        $image->thumb(200, 220,\Think\Image::IMAGE_THUMB_SCALE)->save($path);
        $Lord = D('Admin/Lord');
        $Voet = D('Voet');
        $data['lord_id'] = I('post.lord_id','','intval');
        $data['lord_name'] = I('post.lord_name','','trim');
        $data['user_name'] = I('post.user_name','','trim');
        $data['user_phone'] = I('post.user_phone','','intval');
        $data['lord_manifesto'] = I('post.lord_manifesto','','trim');
        $data['content'] = I('post.content','','trim');
        $data['lord_img'] = $lord_img['choose']['id'];
        if($Lord->create($data)){
            if(empty($_POST['lord_id'])){
                $result = $Lord->add();
                $arr['msg'] = "报名成功";
                $arr['status'] = 1;
            }else{
                $Lord->where(array('lord_id'=>$_POST['lord_id']))->save();
                $info = M('order')->where(array('openid'=>$_SESSION['wx_user'],'status'=>20))->find();
                if(empty($info)){
                    $arr['status'] = 0;
                }else{
                    $arr['status'] = 2;
                }
                $result = $_POST['lord_id'];
                $arr['msg'] = "修改成功";

            }
            $Voet->save_show();
            $this->assign('arr',$arr);
            $this->assign('lord_id',$result);
        }else{
            $this->ajaxReturn(array('status'=>0,'msg'=>$Lord->getError()));
        }
        $map['lord_id'] = array('neq',1);
        $arr = M('lord_user')->where($map)->field(array('lord_id','lord_name','lord_name','user_name','lord_manifesto','voet_num','num'))->order('num asc')->select();
        $this->cache->set('lord_arr',$arr);
        $list = M('lord_user')->where($map)->field(array('lord_id','lord_name','lord_name','user_name','lord_manifesto','lord_img','voet_num'))->order('user_name desc')->limit(0,8)->select();
        $this->cache->set('lord_list',$list);
        $this->assign('lord_id',$result);
        $this->display();
    }

    /* 投票 */
    public function lord_info(){
        $Voet = D('Voet');
        $Lord = D('Admin/Lord');
        $lord_id = I('get.lord_id',0,'intval');
        $Voet->save_num($lord_id);//统计浏览量
        $info = $Lord->get_info($lord_id);
        $all['url']  = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $all['title']= "亲们，".$info['lord_name']."正在参加花木世界社群群主大会，请帮我投票吧！";
        $all['imgUrl']= 'http://'.$_SERVER['HTTP_HOST'].'/Uploads/'.get_cover($info['lord_img'],'path');
        $this->assign('all',$all);
        $this->assign('info',$info);
        $this->display();
    }

    /* 投票 */
    public function lord_voet(){
        $lord_id = I('post.lord_id',0,'intval');
        $Voet = D('Voet');
        $Lord = D('Admin/Lord');
        $result = $Voet->save_voet($lord_id);
        if($result){
            $map['lord_id'] = array('neq',1);
            $arr = M('lord_user')->where($map)->field(array('lord_id','lord_name','lord_name','user_name','lord_manifesto','voet_num','num'))->order('num asc')->select();
            $this->cache->set('lord_arr',$arr);
            $list = M('lord_user')->where($map)->field(array('lord_id','lord_name','lord_name','user_name','lord_manifesto','lord_img','voet_num'))->order('user_name desc')->limit(0,8)->select();
            $this->cache->set('lord_list',$list);
            $info = $Lord->get_info($lord_id);
            $info['status'] = 1;
            $info['msg'] = '投票成功，去找群主要红包吧~';
            $this->ajaxReturn($info);
        }else{
            $this->ajaxReturn(array('status'=>0,'msg'=>'您今天已为TA投满2票，请明天再来！'));
        }
    }

    /* 拉票*/
    public function rank(){
        $Voet = D('Voet');
        $Voet->save_show();
        $map['lord_id'] = array('neq',1);
        if(isset($_GET['lord_name'])){
            $where['lord_name']    =   array('like', '%'.(string)I('lord_name').'%');
            $where['user_name']    =   array('like', '%'.(string)I('lord_name').'%');
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
            $arr = M('lord_user')->where($map)->field(array('lord_id','lord_name','lord_name','user_name','lord_manifesto','voet_num','num'))->order('num asc')->select();
            $list = M('lord_user')->where($map)->field(array('lord_id','lord_name','lord_name','user_name','lord_manifesto','lord_img','voet_num'))->order('user_name desc')->limit(0,8)->select();
        }else{
            $arr = S('lord_arr');
            if(empty($arr)){
                $arr = M('lord_user')->where($map)->field(array('lord_id','lord_name','lord_name','user_name','lord_manifesto','voet_num','num'))->order('num asc')->select();
                S('lord_arr',$arr);
            }
            $list = S('lord_list');
            if(empty($list)){
                $list = M('lord_user')->where($map)->field(array('lord_id','lord_name','lord_name','user_name','lord_manifesto','lord_img','voet_num'))->order('user_name desc')->limit(0,8)->select();
                S('lord_list',$list);
            }
        }
        $this->assign('arr',$arr);
        $this->assign('list',$list);
        $this->display();
    }

    /* 加载更多 */
    public function ajaxRulset(){
        $page = I('post.page',0,'intval');
        $map['lord_id'] = array('neq',1);
        $list = M('lord_user')->order('num asc')->field(array('lord_id','lord_name','lord_name','user_name','lord_manifesto','lord_img','voet_num'))->where($map)->limit($page,8)->order('lord_id asc')->select();
        foreach($list as $k=>$v){
            $list[$k]['lord_img'] = 'Uploads/'.get_cover($v['lord_img'],'path');
        }
        echo json_encode($list);
    }
}